/**
 * Copyright (c) 2014 by it's authors. 
 * All rights reserved. See COPYING, AUTHORS.
 * 
 * This file is part of DanceMania project; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License, Version 3.
 * 
 * DanceMania is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License,
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */
package com.m2dl.niptus.pathtrack.classes;

import java.util.Random;

import android.content.Context;
import android.widget.Toast;

/**
 * Contains more functions for displaying Toasts, etc.
 * 
 * @author John
 *
 */
public class Utils {

	/**
	 * Used for displaying Toasts without overlapping
	 */
	private static Toast mToast;
	
	/**
	 * Shows a toast using a predefined String ID, canceling the current toast if
	 * existent. 
	 * @param context - Activity context
	 * @param content - String Id (strings.xml)
	 */
	public static void showToast(Context context, int content) {
		if (mToast == null) {
			mToast = Toast.makeText(context, content, Toast.LENGTH_LONG);
		}
		
		mToast.setText(content);
		mToast.setDuration(Toast.LENGTH_LONG);
		mToast.show();
	}

	/**
	 * Prepares a toast using the given string message. <code>show()</code> must be called 
	 * in order to display it.
	 * 
	 * @param context	Activity context
	 * @param toast		Current Toast reference (activity defined)
	 * @param content	Message String
	 * @return	The toast to be shown
	 */
	public static void showToast(Context context, String content) {
		if (mToast == null) {
			mToast = Toast.makeText(context, content, Toast.LENGTH_LONG);
		}
		
		mToast.setText(content);
		mToast.setDuration(Toast.LENGTH_LONG);
		
		mToast.show();
	}
	
	/**
	 * Gets the current system time in seconds.
	 * @return	Time in seconds
	 */
	public static int getCurrentTimeSecs() {
		// Some systems might return a different granularity.
		String time = "" + System.currentTimeMillis();
		
		Random r = new Random(System.currentTimeMillis());
		
		// This should never happen
		while (time.length() < 10) {
			time = time + r.nextLong();
		}
		
		return Integer.parseInt(time.substring(0, 10));
	}
}
